package com.meishij.fragment;


import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatDelegate;
import android.text.TextUtils;
import android.view.ContextMenu;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.meishij.MainActivity;
import com.meishij.R;
import com.meishij.activity.Wode_Cut_Image_Activity;
import com.meishij.activity.Wode_LoginActivity;
import com.meishij.customerview.GuoLoadDialog;
import com.meishij.entry.EventBusEntry;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import static android.content.Context.MODE_PRIVATE;
import static com.meishij.R.id.tv_wode_top_username;

/**
 * A simple {@link Fragment} subclass.
 */
public class MineFragment extends Fragment implements View.OnClickListener {


    private ImageView top_userlogo;
    private GuoLoadDialog loadingDialogFirst;
    private TextView top_username;

    public MineFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        EventBus.getDefault().register(this);
        return inflater.inflate(R.layout.fragment_mine, container, false);

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        EventBus.getDefault().unregister(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void handleEventBus(EventBusEntry entry) {
        top_username.setText(entry.getName());
    }


    public Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            loadingDialogFirst.dismiss();
        }
    };

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        top_userlogo = ((ImageView) view.findViewById(R.id.iv_wode_top_userlogo));
        top_username = ((TextView) view.findViewById(tv_wode_top_username));
        top_username.setOnClickListener(this);
        SharedPreferences sp = getContext().getSharedPreferences("config", MODE_PRIVATE);
        if (!TextUtils.isEmpty(sp.getString("lujing", ""))) {
            String lujing = sp.getString("lujing", "");
            Bitmap bitmap = BitmapFactory.decodeFile(lujing);
            top_userlogo.setImageBitmap(bitmap);
        }
        top_userlogo.setOnClickListener(this);
        registerForContextMenu(top_userlogo);
        view.findViewById(R.id.rl_my_setting).setOnClickListener(this);
        loadingDialogFirst = new GuoLoadDialog(getContext(), R.style.MyDialog);
        loadingDialogFirst.setCanceledOnTouchOutside(false);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 100) {
            if (resultCode == getActivity().RESULT_OK) {
                String lujing = data.getStringExtra("lujing");
                Bitmap bitmap = BitmapFactory.decodeFile(lujing);
                top_userlogo.setImageBitmap(bitmap);
                SharedPreferences sp = getContext().getSharedPreferences("config", MODE_PRIVATE);
                SharedPreferences.Editor editor = sp.edit();
                editor.putString("lujing", lujing);
                editor.commit();
            }
        }
        if (requestCode == 1200) {
            if (resultCode == getActivity().RESULT_OK) {
                String name = data.getStringExtra("name");
                top_username.setText(name);
            }
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.rl_my_setting:
                Intent mIntent = new Intent(MainActivity.ACTION_NAME);
                //loadingDialogFirst.show();
                getContext().sendBroadcast(mIntent);
                //handler.sendEmptyMessageDelayed(0,1500);
                break;
            case tv_wode_top_username:
                Intent intent = new Intent(getContext(), Wode_LoginActivity.class);
                //startActivityForResult(intent, 1200);
                startActivity(intent);
                break;
        }
    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);
        menu.add(0, 1, Menu.NONE, "拍照");
        menu.add(0, 2, Menu.NONE, "相册");
    }

    @Override
    public boolean onContextItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case 1:
                break;
            case 2:
                Intent intent = new Intent(getContext(), Wode_Cut_Image_Activity.class);
                startActivityForResult(intent, 100);
                break;
        }
        return super.onContextItemSelected(item);
    }
}
